package edu.sctu.yueding.adapter;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import edu.sctu.yueding.R;
import edu.sctu.yueding.activitys.ShowSelectedPhotoActivity;
import edu.sctu.yueding.bean.Files;
import edu.sctu.yueding.utils.JumpUtils;
import edu.sctu.yueding.utils.ScreenUntils;
import edu.sctu.yueding.utils.httputils.GetVolleyHttp;


public class CommunityItemGridViewAdapter extends BaseAdapter {
    private Activity activity;
    private List<Files> data = new ArrayList<>();
    private ViewHolder viewHolder;
    private View view;
    private int columns;
    private ArrayList<String> imgPath = new ArrayList<String>();


    public CommunityItemGridViewAdapter(Activity activity, List<Files> data, int columns) {
        this.activity = activity;
        this.data = data;
        this.columns = columns;
        for (Files file : data) {
            imgPath.add(file.getUrl());
        }
    }

    @Override
    public int getCount() {
        if (data != null && data.size() > 0) {
            return data.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            viewHolder = new ViewHolder();
            view = LayoutInflater.from(activity).inflate(R.layout.item_commounity_gridview, null);
            viewHolder.imageView = (ImageView) view.findViewById(R.id.item_community_list_gridview_img);
            viewHolder.linearLayout = (LinearLayout) view.findViewById(R.id.item_community_list_gridview_lay);

            view.setTag(viewHolder);
        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }

        if (data.size() == 1) {

            double w = ScreenUntils.getScreenWidth(activity);

            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((int) (w * 0.6f),
                    500);

            viewHolder.imageView.setLayoutParams(lp);
        } else {

        }


        if (!data.get(position).getUrl().equals("") && data.get(position).getUrl() != null) {
            GetVolleyHttp.getInstance(activity).setImageLoader(viewHolder.imageView,
                    R.drawable.photo_pictures_no,
                    R.drawable.photo_pictures_no,
                    data.get(position).getUrl());
        }
        viewHolder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putBoolean("isURL", true);
                bundle.putInt("position", position);
                bundle.putStringArrayList("selectedImgPath", imgPath);
                JumpUtils.jumpActivity(activity, ShowSelectedPhotoActivity.class, bundle);
            }
        });
        return view;
    }

    private class ViewHolder {
        ImageView imageView;
        LinearLayout linearLayout;
    }

//    private
}
